package com.example.administrator.zhjt.activity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.administrator.zhjt.R;
import com.example.administrator.zhjt.adapter.RoadAdapter;
import com.example.administrator.zhjt.bean.Event;
import com.example.administrator.zhjt.bean.Road;
import com.example.administrator.zhjt.util.MyVolley;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Handler;

public class RoadActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private static final int NOTIFICATION_FLAG = 1;
    private List<Road> roadList = new ArrayList<>();
    private RoadAdapter roadAdapter;
    private Spinner spinnerRoad;
    private ListView listRoad;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.road_layout);
        initView();
        MyVolley.init(this);
        roadAdapter = new RoadAdapter(roadList, this);

        roadList.add(new Road());
        roadList.add(new Road());
        roadList.add(new Road());
        roadList.add(new Road());
        roadList.add(new Road());

        listRoad.setAdapter(roadAdapter);
        handler.post(runnable);

        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event){
        if(event != null){
            System.out.println(event.getTag().toString());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void Post() {
        MyVolley.getRoadStatus(1, "Admin", new MyVolley.OnResultListener() {
            @Override
            public void done(JSONObject result) {
                try {
                    if (result.getString("RESULT").equals("S")){
                        Road road=new Road();
                        road.setRoadId(1);
                        roadList.set(0,road);
                        road.setBalance(result.getInt("Balance"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError error) {

            }
        });

        MyVolley.getRoadStatus(2, "Admin", new MyVolley.OnResultListener() {
            @Override
            public void done(JSONObject result) {
                try {
                    if (result.getString("RESULT").equals("S")){
                        Road road=new Road();
                        road.setRoadId(2);
                        roadList.set(1,road);
                        road.setBalance(result.getInt("Balance"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError error) {

            }
        });

        MyVolley.getRoadStatus(3, "Admin", new MyVolley.OnResultListener() {
            @Override
            public void done(JSONObject result) {
                try {
                    if (result.getString("RESULT").equals("S")){
                        Road road=new Road();
                        road.setRoadId(3);
                        roadList.set(2,road);
                        road.setBalance(result.getInt("Balance"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError error) {

            }
        });

        MyVolley.getRoadStatus(4, "Admin", new MyVolley.OnResultListener() {
            @Override
            public void done(JSONObject result) {
                try {
                    if (result.getString("RESULT").equals("S")){
                        Road road=new Road();
                        road.setRoadId(4);
                        roadList.set(3,road);
                        road.setBalance(result.getInt("Balance"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError error) {

            }
        });

        MyVolley.getRoadStatus(5, "Admin", new MyVolley.OnResultListener() {
            @Override
            public void done(JSONObject result) {
                try {
                    if (result.getString("RESULT").equals("S")){
                        Road road=new Road();
                        road.setRoadId(5);
                        roadList.set(4,road);
                        road.setBalance(result.getInt("Balance"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError error) {

            }
        });
        update();
        notification();
    }

    private void notification() {
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            PendingIntent pendingIntent=PendingIntent.getActivity(this,0,new Intent(this,RoadActivity.class),0);
            Notification notification=new Notification.Builder(this)
                    .setSmallIcon(R.drawable.guide01)
                    .setContentTitle("紧急通知")
                    .setContentText("疏通交通")
                    .setContentIntent(pendingIntent)
                    .build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            manager.notify(NOTIFICATION_FLAG, notification);

    }


    private void initView() {
        spinnerRoad = (Spinner) findViewById(R.id.spinner_road);
        spinnerRoad.setOnItemSelectedListener(this);
        listRoad = (ListView) findViewById(R.id.list_road);
    }

    android.os.Handler handler=new android.os.Handler();
    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            Post();
            handler.postDelayed(runnable,3000);
        }
    };

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        update();
    }

    private void update() {
        switch (spinnerRoad.getSelectedItemPosition()){
            case 0:
                Collections.sort(roadList);
                break;
            case 1:
                Collections.sort(roadList);
                Collections.reverse(roadList);
                break;
        }
        for (Road road : roadList) {
            if(road.getBalance() > 3){
                notification();
            }
        }

        roadAdapter.notifyDataSetChanged();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
